package org.xvergabe.forms.poc.pdfgenerator;

import java.awt.Color;
import java.awt.Rectangle;

import org.xvergabe.forms.poc.helper.Labeller;
import org.xvergabe.xsd.forms.components.x20.DynamicCheckGroup;
import org.xvergabe.xsd.forms.components.x20.DynamicCheckGroup.Options.Option;

import com.lowagie.text.Anchor;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PushbuttonField;

/**
 * Class to write a {@link DynamicCheckGroup} to PDF.
 * 
 * @author hombergs
 * 
 */
public class DynamicCheckGroupPdfWriter implements
		XmlPdfWriter<DynamicCheckGroup> {

	@Override
	public void writePdfFromXml(DynamicCheckGroup checkgroup, PdfWriter writer,
			Document document) {

		Option[] options = checkgroup.getOptions().getOptionArray();

		int[] widths = { 20 };

		try {
			PdfPTable table = new PdfPTable(1);
			Font f = new Font();
			f.setSize(12);
			f.setStyle(Font.BOLD);
			Phrase label = new Phrase(new Chunk(
					Labeller.getLabelFor(checkgroup), f));

			table.setWidths(widths);
			table.setSpacingBefore(30);
			table.setSpacingAfter(0);

			PdfPCell cell = new PdfPCell(label);
			cell.setColspan(1);
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			document.add(table);

			for (int i = 0; i < options.length; i++) {

				PdfPTable innerTable = new PdfPTable(1);
				innerTable.setWidths(widths);
				innerTable.setSpacingAfter(0);
				innerTable.setSpacingBefore(0);

				// Label
				cell = new PdfPCell(new Phrase(options[i].getValue()));
				cell.setBorder(PdfPCell.NO_BORDER);
				innerTable.addCell(cell);

				// fuege button hinzu, um einen neuen Eintrag hinzuzufügen
				if(i == (options.length -1)){
					Font font = new Font();
					font.setColor(new Color(0, 0, 255));
					// erstmal nur als Link, Buttons sind schwierig zu platzieren...
					Anchor link = new Anchor("Weiteren Eintrag hinzufügen", font);
					cell = new PdfPCell(link);
					cell.setBorder(PdfPCell.NO_BORDER);
					innerTable.addCell(cell);
				}

				document.add(innerTable);
			}
			
			
			
		} catch (DocumentException e) {
			throw new RuntimeException(e);
		}
	}

}
